<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Account information class.
 *
 * Account information's CRUD.
 */
class App_info_model extends CI_Model
{
	private $db_name = 'k9deploy_new';
	private $table = 'app_info';
	private $db = null;
	
	/**
	 * Constructor
	 *
	 * Sets the database used for saving account information.
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->db = $this->load->database($this->db_name, TRUE);
	}
	
	/**
	 * Get account's information
	 *
	 * @param	array	where condition
	 * @return	object
	 */
	public function get($where)
	{
		$query = $this->db->get_where($this->table, $where);
		return $query->row_array();
	}
	
	
	// ------------------------------------------------------------------------
	
	/**
	 * 根据用户名或者用户UID称获取该用户完整的信息
	 *
	 * @access  public
	 * @param   mixed
	 * @return  object
	 */
	public function get_full_user_by_username($username = '', $type = 'username')
	{
		if ($type == 'uid')
		{
			$this->db->where('uid', $username);
		}
		else
		{
			$this->db->where('uname', $username);
		}
		return $this->db->select("uid, rname, email, pwd")
						->from($this->table)
						->get()
						->row();
	}


	public function get_appid($app)
	{
		$appid = $this->db->get_where($this->table,array('app_name' => $app))->result_array();
		return $appid[0]['id'];
	}
		
	public function get_app_name($appid)
	{
		$appid = $this->db->get_where($this->table,array('id' => $appid))->result_array();
		return $appid[0]['app_name'];
	}

	public function get_git_url($appid)
        {
                $appid = $this->db->get_where($this->table,array('id' => $appid))->result_array();
                return $appid[0]['git_url'];
        }

	public function get_ci_flag($appid)
        {
                $appid = $this->db->get_where($this->table,array('id' => $appid))->result_array();
                return $appid[0]['ci_flag'];
        }

	public function get_nci_db_flag($appid)
        {
                $appid = $this->db->get_where($this->table,array('id' => $appid))->result_array();
                return $appid[0]['nci_db_flag'];
        }

	
	// ------------------------------------------------------------------------
	
	/**
	 * 根据用户ID获取用户信息
	 *
	 * @access  public
	 * @param   int
	 * @return  object
	 */
	public function get_user_by_uid($uid = 0)
	{
		return $this->db->where('uid', $uid)->get($this->table)->row();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 根据用户名获取用户信息
	 *
	 * @access  public
	 * @param   string
	 * @return  object
	 */
	public function get_user_by_name($name)
	{
		return $this->db->where('uname', $name)->get($this->table)->row();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 用户自己密码
	 *
	 * @access  public
	 * @return  bool
	 */
	public function update_user_password()
	{
		$data['pwd'] = $this->input->post('new_pass', TRUE);
		$data['salt'] = substr(sha1(time()), -10);
		$data['pwd'] = sha1($data['password'].$data['salt']);
		return $this->db->where('uid', $this->session->userdata('uid'))->update($this->table, $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 获取用户组列表
	 *
	 * @access  public
	 * @return  object
	 */
	public function get_roles()
	{
		$roles = array();
		foreach ($this->db->select('id, name')->where('id <>', 1)->get($this->db->dbprefix('roles'))->result_array() as $v)
		{
			$roles[$v['id']] = $v['name'];
		}
		return $roles;
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 获取用户数
	 *
	 * @access  public
	 * @param   int
	 * @return  int
	 */
	public function get_users_num($role_id = 0)
	{
		$this->db->where('uid <>', 1);
		if ($role_id)
		{
			$this->db->where('role', $role_id);
		}
		return $this->db->count_all_results($this->table);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 获取某个用户组下所有用户
	 *
	 * @access  public
	 * @param   int
	 * @param   int
	 * @param   int
	 * @return  object
	 */
	public function get_users($role_id = 0, $limit = 0, $offset = 0)
	{
		$this->db->where("uid <>", 1);
		if($role_id > 0)
			$this->db->where("role", $role_id);
		if ($limit)
		{
			$this->db->limit($limit);
		}
		if ($offset)
		{
			$this->db->offset($offset);
		}
		return $this->db->from($this->table)
						->get()
						->result();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 添加用户
	 *
	 * @access  public
	 * @param   array
	 * @return  bool
	 */
	public function add_user($data)
	{
		$data['salt'] = substr(sha1(time()), -10);
		$data['pwd'] = sha1($data['pwd'].$data['salt']);
		return $this->db->insert($this->table, $data);
	}

	public function add_app($data)
	{
		foreach($data as $k => $v){
			if($v==""){
				return $k;
			}else{
				continue;
			}
		}
		if(count($this->db->get_where($this->table,array('app_name'=>$data['app_name']))->result_array())=='0'){
			$this->db->insert($this->table,$data);
			return 0;
		}else{
			return 100;
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 修改用户
	 *
	 * @access  public
	 * @param   int
	 * @param   array
	 * @return  bool
	 */
	public function edit_user($uid, $data)
	{
		if (isset($data['pwd']))
		{
			$data['salt'] = substr(sha1(time()), -10);
			$data['pwd'] = sha1($data['pwd'].$data['salt']);
		}
		return $this->db->where('uid', $uid)->update($this->table, $data);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 删除用户
	 *
	 * @access  public
	 * @param   uid
	 * @return  bool
	 */
	public function del_user($uid)
	{
		return $this->db->where('uid', $uid)->delete($this->table);
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * 获取用户组下用户数目
	 *
	 * @access  public
	 * @param   int
	 * @return  int
	 */
	public function get_role_user_num($id)
	{
		return $this->db->where('role', $id)->count_all_results($this->table);
	}

	 /**
         * get user name
         *
         */
        public function get_username($uid = 'anonymous' ){
                $user_info = $this->db->get_where($this->table,array('uid' => $uid))->result_array();
                return $user_info[0]['rname'];
        }

	/*
         *
         * apps
         *
         *
        */
	public function get_app_info(){
		$app_info = $this->db->get_where($this->table)->result_array();
		return $app_info;
	}

	/*
         *
         * apps
         *
         *
        */
        public function get_apps($class = '3'){
                if($class != 3){
                        $where = array(
                                'type' => $class
                        );
                        $app_info = $this->db->get_where($this->table,$where)->result_array();
			if(count($app_info)==0){
				$app_info = $this->db->get_where($this->table)->result_array();
				foreach($app_info as $k => $v){
					$apps[] = $v['app_name'];
				}
			}else{
				foreach($app_info as $k => $v){
                                        $apps[] = $v['app_name'];
                                }
			}
			return $apps;
                }else{
                        $app_info = $this->db->get_where($this->table)->result_array();
			foreach($app_info as $k => $v){
                                $apps[] = $v['app_name'];
                        }
                        return $apps;
                }
        }


}
